﻿using Jayden.Dll.Core.Exceptions;
using Jayden.Dll.Core.Persistence.Enums;
using Jayden.Dll.Core.Persistence.Models;

namespace Jayden.Dll.Core.Persistence
{
	public abstract class ObjectBase : Object
	{
		private ObjectId m_ObjectId;
		private UnitOfWork m_UnitOfWork;
		private ObjectState m_State = ObjectState.New;
		private Entity m_Entity;

		public ObjectBase(UnitOfWork uow, long id) : this(uow, new object[] { id }) { }
		public ObjectBase(UnitOfWork uow, params object[] id)
		{
			if (uow == null)
				throw new Exception("cannot create object without unit of work.");
			m_UnitOfWork = uow;
			m_ObjectId = new ObjectId(Type, id);
			m_State = ObjectState.New;
		}

		public virtual string EntityFullName { get { return GetType().FullName; } }
		public Entity Entity
		{
			get
			{
				if(m_Entity == null)
					m_Entity = m_UnitOfWork.Model.GetEntity(EntityFullName);
				return m_Entity;
			}
		}

		public UnitOfWork UnitOfWork { get { return m_UnitOfWork; } }
		public ObjectId ObjectId { get { return m_ObjectId; } }
		public ObjectState State { get { return m_State; } set { m_State = value; } }
		public ObjectType Type { get { return Entity.ObjectType; } }

	}
}
